import { Injectable } from '@angular/core';
import { ToastController, LoadingController, Loading, AlertController } from 'ionic-angular';

/*
  Generated class for the UiserviceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class UiserviceProvider {
  loader: Loading;
  constructor(private toastCtrl: ToastController, private loadingCtrl: LoadingController, private alertCtrl: AlertController) {
    console.log('Hello UiserviceProvider Provider');
  }

  public showToast(message?: string, duration?) {
    if (duration == null) {
      duration = 3000;
    }
    const toast = this.toastCtrl.create({
      message: message,
      duration: duration
    });
    toast.present();
  }

  showLoading() {
    this.loader = null;
    if (this.loader == null) {
      this.loader = this.loadingCtrl.create({
        content: "正在加载...",
      });
    }
    this.loader.present();
  }

  hideLoading() {
    if (this.loader != null) {
      this.loader.dismiss().then(() => {
        this.loader = null;
      });
    }
  }

  showAlert(message:string) {
    const alert = this.alertCtrl.create({
      title: '提示!',
      subTitle: message,
      buttons: ['确定']
    });
    alert.present();
  }

}
